package com.starbaba.externalpage.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.starbaba.base.dao.LoanManagerBaseDao;
import com.starbaba.core.web.page.PageRequest;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.externalpage.model.ExternalDownBean;
import com.starbaba.externalpage.model.ExternalPageModel;
import com.starbaba.externalpage.model.ExternalProduct;
import com.starbaba.externalpage.model.ExternalResult;
import com.starbaba.externalpage.service.ExternalPageService;

public class ExternalPageServiceImpl extends LoanManagerBaseDao implements ExternalPageService {

	@Override
	public void showpage(Map<String, Object> paramsMap) {
		String sql="select id as activity_id,name as activity_name from loan_product_info where status=1 and union_type!=0";
		try {
			List<ExternalPageModel> list = getDao().list(sql, ExternalPageModel.class);
			paramsMap.put("productList", list);
		} catch (AppException e) {
			LogService.error("后台配置外部直接联登推广页准备数据失败！",e);
			e.printStackTrace();
		}
		
	}

	@Override
	public List<ExternalPageModel> getExternalPageList(PageRequest pageRequest, PageResponse pageResponse,
			Map<String, Object> paramsMap) throws Exception {
		
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();
		
		String product_id = (String) paramsMap.get("product_id");
		
		StringBuffer sql=new StringBuffer();
		sql.append("select id as activity_id,name as activity_name,logo,external_num,external_status from loan_product_info where status=1 and union_type!=0");
		
		if(!StringUtils.isEmpty(product_id)){
			sql.append(" and id="+product_id);
		}
		
		sql.append(" order by external_num desc ");
		
		int count = getDao().getInt("select count(z.activity_id) from ("+sql.toString()+") z");
		
		if(start>0&&length>0&&sql.length()>0){
			sql.append(" limit "+start+", "+length);
		}
		
		List<ExternalPageModel> list = getDao().list(sql.toString(), ExternalPageModel.class);
		
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		
		return list;
	}

	@Override
	public ExternalResult saveExternalInfo(String activity_id, String external_num, String status,String android_url,String ios_url) {
		
		String sql="select union_type from loan_product_info where id=?";

		ExternalResult result = new ExternalResult();
		
		ExternalProduct product = new ExternalProduct();	
		product.setId(Integer.valueOf(activity_id));
		product.setExternal_num(external_num);
		product.setExternal_status(status);
		
		try {
			ExternalDownBean bean = getDao().get(sql, ExternalDownBean.class, activity_id);
			bean.setActivity_id(Integer.valueOf(activity_id));
			bean.setAndroid_url(android_url);
			bean.setIos_url(ios_url);
			
			saveOrUpdateDownBean(bean);
				
			boolean success = getDao().updateReturnResult(product, new String[]{"id"});
			
			if(success){	
				result.setResult("更新成功");
			}else{
				result.setResult("更新失败");	
			}
				
		} catch (AppException e) {
			LogService.error("直接外部页更新失败",e);
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public ExternalDownBean getUrl(String activity_id) {
		String sql="select * from loan_external_product_list where activity_id=?";
		ExternalDownBean bean = null;
		try {
			bean = getDao().get(sql, ExternalDownBean.class, activity_id);
		} catch (AppException e) {
			LogService.error(e);
			e.printStackTrace();
		}
		return bean;
	}
	
	public void saveOrUpdateDownBean(ExternalDownBean bean){
		Integer activity_id = bean.getActivity_id();
		String sql="select count(activity_id) from loan_external_product_list where activity_id=?";
		try {
			
			int num = getDao().getInt(sql, activity_id);
			if(num>0){
				getDao().update(bean, new String[]{"activity_id"});
			}else{
				getDao().add(bean);
			}
				
		} catch (AppException e) {
			LogService.error(e);
			e.printStackTrace();
		}
	}

}
